<template>
  <el-row class="warp">
    <el-col :span="24" class="warp-breadcrum">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }"><b>首页</b></el-breadcrumb-item>
        <el-breadcrumb-item>设置</el-breadcrumb-item>
        <el-breadcrumb-item>修改密码</el-breadcrumb-item>
      </el-breadcrumb>
    </el-col>

    <el-col :span="12" class="warp-main-user" v-loading="loading">
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item  v-show="isShowOldPwd" label="原密码" prop="oldPwd">
          <el-input type="password" v-model="form.oldPwd"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPwd">
          <el-input type="password" v-model="form.newPwd"></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPwd">
          <el-input type="password" v-model="form.confirmPwd"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="float: right" @click="handleChangepwd">提交</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>
<script>
  import UserAPI from '@/api/api_user';
  import valid from '@/utils/valid.js'
  export default{
    data(){
      var validatePass2 = (rule, value, callback) => {
       if (value !== this.form.newPwd) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        loading: false,
        isShowOldPwd: true,
        form: {
          oldPwd: '',
          newPwd: '',
          confirmPwd: ''
        },
        rules: {
          oldPwd: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ],
          newPwd: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {validator: valid.isPassword, trigger: 'blur'},
          ],
          confirmPwd: [
            {required: true, message: '请输入密码', trigger: 'blur'},
            {validator: valid.isPassword, trigger: 'blur'},
            {validator: validatePass2, trigger: 'blur'}
          ]
        }
      }
    },
    mounted() {
        debugger
        let isFirstLogin = this.$route.params.isFirstLogin
        if(isFirstLogin === '0'){
          this.form.oldPwd = '123456'
          this.isShowOldPwd = false
        }
        this.handleSearch()
    },
    methods: {
      handleChangepwd() {
        let _this = this
        this.$refs.form.validate((valid) => {
          if (valid) {
            _this.loading = true;
            UserAPI.updatePwd(this.form).then(result => {
              _this.loading = false;
              if(result.code === '0000'){
                _this.$message({message: "密码修改成功", duration: 2000, type: 'success'});
              } else {
                _this.$message({message: result.message, duration: 2000, type: 'error'});
              }
            })
          }
        })
      }
    }
  }
</script>
